package com.deram.ctin.rest.web.controller.web;

import com.deram.ctin.common.bean.Page;
import com.deram.ctin.common.bean.PageResult;
import com.deram.ctin.common.utils.ValidateUtils;
import com.deram.ctin.dependencies.rest.base.BaseController;
import com.deram.ctin.rest.web.result.web.WebEnterInfoResult;
import com.deram.ctin.serv.business.api.*;
import com.deram.ctin.serv.business.entity.*;
import com.deram.ctin.serv.enter.api.EnterInfoApi;
import com.deram.ctin.serv.enter.entity.EnterInfo;
import com.deram.ctin.serv.search.entity.solr.api.SolrEnterInfoApi;
import com.deram.ctin.serv.search.entity.solr.bean.SolrEnterInfo;
import com.deram.ctin.serv.search.entity.solr.search.WebEnterSearchParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.text.ParseException;
import java.util.List;

/**
 * Created by xhw on 2017/09/13.
 */

@Api(tags = {"web - 企业接口"})
@RestController
@RequestMapping("/web/enter")
public class WebEnterController extends BaseController {

    @Autowired
    private EnterInfoApi enterInfoApi;

    @Autowired
    private BusinessInfoApi businessInfoApi;

    @Autowired
    private SolrEnterInfoApi solrEnterInfoApi;

    @ApiOperation(value = "搜索")
    @GetMapping("/search")
    public PageResult<SolrEnterInfo> search(WebEnterSearchParam param, Page page) throws ParseException {
        ValidateUtils.notEmpty(param.getKeywords(),"搜索关键字");
        return solrEnterInfoApi.searchByKeywords(param,page.getPageNo(),page.getPageSize());
    }

    @ApiOperation(value = "详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, paramType = "path")
    })
    @GetMapping("/{id}")
    public WebEnterInfoResult info(@PathVariable Long id){
        WebEnterInfoResult result = new WebEnterInfoResult();
        EnterInfo enterInfo = enterInfoApi.findById(id);
        BusinessInfo businessInfo = businessInfoApi.find(enterInfo.getId());
        result.setEnterInfo(enterInfo);
        result.setBusinessInfo(businessInfo);
        List<Integer> annualYearList = businessInfoApi.listYear(id);
        result.setAnnualYearList(annualYearList);
        return result;
    }
}
